package com.ihr360.oneclick.service.impl;

import com.ihr360.oneclick.entity.OneClickTerm;
import com.ihr360.oneclick.service.OneClickTermService;
import com.ihr360.oneclick.service.SpreadsheetRowService;
import com.ihr360.oneclick.service.StaffRecordService;
import com.ihr360.oneclick.service.WeChatApiService;
import com.ihr360.oneclick.vo.StaffMonthMessageVo;
import com.ihr360.oneclick.vo.WechatFunctionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tom
 * @date 18-1-12
 */
@Service
public class WeChatApiServiceImpl implements WeChatApiService {

    @Autowired
    private OneClickTermService oneClickTermService;
    @Autowired
    private SpreadsheetRowService spreadsheetRowService;
    @Autowired
    private StaffRecordService staffRecordService;

    @Override
    public List<WechatFunctionVo> getOneClickFunctions(String companyId) {
        List<OneClickTerm> oneClickTermList = oneClickTermService.getTermListByEnable(companyId, true);
        List<WechatFunctionVo> wechatFunctionVoList = new ArrayList<>();
        oneClickTermList.forEach(oneClickTerm -> {
            WechatFunctionVo wechatFunctionVo = new WechatFunctionVo();
            wechatFunctionVo.setFunctionId(oneClickTerm.getId());
            wechatFunctionVo.setFunctionName(oneClickTerm.getWechatMenuName());
            wechatFunctionVoList.add(wechatFunctionVo);
        });
        return wechatFunctionVoList;
    }

    @Override
    public StaffMonthMessageVo getFunctionStaffDataByMonth(String companyId, String functionId, String mobileNo, String month) {
        /*StaffRecord staffRecord = staffRecordService.
                getStaffRecordByCompanyIdAndMobileNo(companyId, mobileNo);
        if (staffRecord == null  || !staffRecord.getAllow()) {
            //员工名录为空，说明该用户没有查看发布信息权限
            return null;
        }*/
        return spreadsheetRowService.getPublishSpreadSheetRowsByTermAndMobileNoAndMonth(companyId, functionId, mobileNo, month);
    }

    @Override
    public List<String> getFunctionStaffDataMonthList(String companyId, String functionId, String mobileNo) {
        List<String> monthList = new ArrayList<>();
        /*StaffRecord staffRecord = staffRecordService.
                getStaffRecordByCompanyIdAndMobileNo(companyId, mobileNo);
        if (staffRecord == null || !staffRecord.getAllow()) {
            // 员工名录为空， 或者员工没有查看权限，不返回数据
            return monthList;
        }*/
        List<StaffMonthMessageVo> messageList = spreadsheetRowService.getPublishSpreadSheetRowsByTermAndMobileNoGroupByMonth(companyId, functionId, mobileNo);
        if (CollectionUtils.isEmpty(messageList)) {
            return monthList;
        }
        messageList.forEach(staffMonthMessage ->
                monthList.add(staffMonthMessage.getMonth())
        );
        return monthList;
    }
}
